#do some cleanup jobs
rm -rf ~/remotePublish*
rm -rf /tmp/md5values

#get ftp directory
ftpdir=`cat $1 | grep ftpdir | awk -F , '{print $2}'`
function genmd5values(){
        find $1 -name '*.*' | while read ln; do

        md5sum $ln >> /tmp/md5values

        done
}
genmd5values $ftpdir



# pass new file to remote and execute publish script
#cat $1 | grep ip | awk -F , '{print $2}'

function remoteExec(){
        time=`date +%Y%m%d_%H%I%S`
        ssh $1 mkdir -pv ~/backup/$time/
        ssh $1 mkdir -pv ~/remotePublishDir/$time/
        ssh $1 mkdir -pv ~/remotePublishScript/$time/
        ftpdir=`cat $2 | grep ftpdir | awk -F , '{print $2}'`
        appdir=`cat $2 | grep appdir | awk -F , '{print $2}'`

        ssh $1 $appdir/* ~/backup/$time/
        scp -rp $ftpdir/* $1:~/remotePublishDir/$time/
        scp -rp /tmp/md5values $1:~/remotePublishScript/$time/

        scp -rp ~/pubmain/remoteScript.sh $1:~/remotePublishScript/$time/

        ssh $1 ~/remotePublishScript/$time/remoteScript.sh ~/remotePublishDir/$time $appdir ~/remotePublishScript/$time/md5va
lues

}

cat $1 | grep ip | awk -F , '{for(i=2;i<=NF;i++) print $i}' | while read ip; do
        remoteExec $ip $1
done
